package com.tsingteng.cosfun.http;

import android.content.Context;
import android.util.Log;


import com.tsingteng.cosfun.common.Config;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 描述:网络请求
 * 创建人: 肖利
 *时间: 2018/7/30
 * qq: (872179966)
 */
public class RxRetrofit {
    private Retrofit retrofit;
    private static ApiService apiService;

    private static final class Holder {
        private static final RxRetrofit INSTANTE = new RxRetrofit();
    }

    //初始化retrofit
    public void initRxRetrofit(Context context, String baseUrl) {
        // 日志显示级别
        HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.i("gusheng","gusheng = "+message);
            }
        });
        loggingInterceptor.setLevel(level);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //链接超时
                .connectTimeout(10, TimeUnit.SECONDS)
                //读取超时
                .readTimeout(30, TimeUnit.SECONDS)
                //cookie 请求头  签名等信息
                .addInterceptor(new RequestInterceptor())
                .addInterceptor(new Interceptor() { // Response interceptor，打印原始报文起始的一部分，方便调试
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Response response = chain.proceed(chain.request());
                        ResponseBody responseBodyHead = response.peekBody(128);
                        Log.i("RxReqResInterceptor", "" + responseBodyHead.string());
                        return response;
                    }
                })
                //缓存
                .cache(new Cache(context.getExternalFilesDir(Config.HTTP_CACHE), Config.HTTP_CACHE_SIZE))
                .addInterceptor(loggingInterceptor)//d打印报文信息
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    public static RxRetrofit getInstance() {
        return Holder.INSTANTE;
    }

    public static ApiService getApiSever() {
        if (apiService==null) {
            throw new IllegalStateException("请先初始化");
        }
        return apiService;
    }
}
